<template>
  <div class="app-table">
    <a-table :columns="columns" :data-source="data"></a-table>
  </div>
</template>

<script lang="ts">
import { computed, defineComponent, reactive, ref } from "vue";
import { BASE_URL, myPost, Response } from "@/utils/request";
import { useStore } from "vuex";
import { message } from "ant-design-vue";

export default defineComponent({
  name: "LendTable",
  setup() {
    const data = ref([] as any[]);
    const store = useStore();
    const name = computed(() => store.getters.name);
    console.log(name);
    // const columns = computed(() => getColumns(data.value));
    const columns = [
      {
        dataIndex: "stuffRealName",
        key: "stuffRealName",
        title: "姓名"
      },
      {
        dataIndex: "stuffNum",
        key: "stuffNum",
        title: "学（工）号"
      },
      {
        dataIndex: "college",
        key: "college",
        title: "学（书）院"
      },
      {
        dataIndex: "lesson",
        key: "lesson",
        title: "课程名称"
      },
      {
        dataIndex: "classroom",
        key: "classroom",
        title: "教室"
      }
    ];
    let respBody = {
      isSuccess: false,
      msg: "",
      data: []
    };
    const curState = reactive({
      out: 0,
      remain: 0,
      max: 0
    });

    function queryBorrow() {
      myPost(BASE_URL + "/admin/queryBorrow", {
        officerId: store.getters.id
      })
        .then((resp: Response) => {
          respBody = resp.data;
          if (respBody["isSuccess"]) {
            data.value = (respBody["data"] as any[]).map((item, key) => ({
              ...item,
              key
            }));
            curState.out = respBody.data.length;
          } else {
            message.error({ content: respBody["msg"], duration: 2 });
          }
        })
        .catch((error: any) => {
          console.log(error);
          message.error({ content: error, duration: 2 });
        });
    }

    function queryRemain() {
      myPost(BASE_URL + "/officer/queryRemain", {
        officerId: store.getters.id
      })
        .then((resp: Response) => {
          respBody = resp.data;
          if (respBody["isSuccess"]) {
            curState.remain = respBody.data[0];
          } else {
            message.error({ content: respBody["msg"], duration: 2 });
          }
        })
        .catch((error: any) => {
          console.log(error);
          message.error({ content: error, duration: 2 });
        });
    }

    function queryMax() {
      myPost(BASE_URL + "/officer/queryMax", {
        officerId: store.getters.id
      })
        .then((resp: Response) => {
          respBody = resp.data;
          if (respBody["isSuccess"]) {
            curState.max = respBody.data[0];
          } else {
            message.error({ content: respBody["msg"], duration: 2 });
          }
        })
        .catch((error: any) => {
          console.log(error);
          message.error({ content: error, duration: 2 });
        });
    }

    function update() {
      queryBorrow();
      queryRemain();
      queryMax();
    }

    update();

    return {
      curState,
      columns,
      data,
      name
    };
  }
});
</script>
<style scoped></style>
